<template>
  <el-dialog
    class="mod-coupon-add-or-update"
    :title="dataForm.couponId ? $t('coupon.modifyCoupon') : $t('coupon.newCoupon')"
    :close-on-click-modal="false"
    :before-close="beforeClose"
    :visible.sync="visible"
  >
    <el-form
      size="small"
      v-if="show"
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      :label-width="this.$i18n.t('language') === 'language' ? '170px' : '150px'"
    >
      <el-form-item :label="$t('chain.name')" prop="name">
        <el-input
          v-model.trim="dataForm.name"
          :placeholder="$t('chain.name')"
          maxlength="20"
        ></el-input>
      </el-form-item>
      <el-form-item :label="this.$i18n.t('chain.code')" prop="code">
        <el-input
          v-model.trim="dataForm.code"
          :placeholder="$t('chain.code')"
          maxlength="20"
        ></el-input>
      </el-form-item>

      <el-form-item :label="this.$i18n.t('chain.icon')" prop="icon">
        <imgs-upload v-model="dataForm.icon" :limit="1" :size="'380*96'" />
        <div class="el-form-item-tips">{{ $t("webInfoConfig.imgsTip") + '380*96' + $t("webInfoConfig.px") }}</div>
      </el-form-item>

      <el-form-item :label="this.$i18n.t('chain.apiUser')" prop="apiUser">
        <el-input
          v-model.trim="dataForm.apiUser"
          :placeholder="$t('chain.apiUser')"
          maxlength="20"
        ></el-input>
      </el-form-item>

      <el-form-item :label="this.$i18n.t('chain.apiPassword')" prop="apiPassword">
        <el-input
          v-model.trim="dataForm.apiPassword"
          :placeholder="$t('chain.apiPassword')"
          maxlength="20"
        ></el-input>
      </el-form-item>

      <el-form-item :label="this.$i18n.t('chain.address')" prop="address">
        <el-input
          v-model.trim="dataForm.address"
          :placeholder="$t('chain.address')"
          maxlength="20"
        ></el-input>
      </el-form-item>

      <el-form-item :label="this.$i18n.t('chain.mnemonic')" prop="mnemonic">
        <el-input
          v-model.trim="dataForm.mnemonic"
          :placeholder="$t('chain.mnemonic')"
          maxlength="20"
        ></el-input>
      </el-form-item>

      <el-form-item :label="this.$i18n.t('chain.middleAddress')" prop="middleAddress">
        <el-input
          v-model.trim="dataForm.middleAddress"
          :placeholder="$t('chain.middleAddress')"
          maxlength="20"
        ></el-input>
      </el-form-item>

      <el-form-item :label="this.$i18n.t('chain.middleMnemonic')" prop="middleMnemonic">
        <el-input
          v-model.trim="dataForm.middleMnemonic"
          :placeholder="$t('chain.middleMnemonic')"
          maxlength="20"
        ></el-input>
      </el-form-item>

      <el-form-item :label="$t('chain.status')" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">{{$t("chain.enable")}}</el-radio>
          <el-radio :label="0">{{$t("chain.disable")}}</el-radio>
        </el-radio-group>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <div class="default-btn" @click="show = false;visible = false">{{$t("coupon.cancel")}}</div>
      <div class="default-btn primary-btn" @click="dataFormSubmit()">{{$t("coupon.confirm")}}</div>
    </span>
  </el-dialog>
</template>

<script>
import ImgsUpload from '@/components/img-upload'
import {validNoEmptySpace} from '@/utils/validate'
export default {
  data () {
    var validateName = (rule, value, callback) => {
      if (validNoEmptySpace(value)) {
        callback(new Error(this.$i18n.t('shopProcess.inputAllSpace')))
      } else {
        this.$http({
          url: this.$http.adornUrl('/platform/blockchain/checkBlockchainName'),
          method: 'GET',
          params: this.$http.adornParams({
            name: value,
            id: this.dataForm.id
          })
        }).then(({ data }) => {
          if (data) {
            callback(new Error(this.$t('shopProcess.shopNameRule')))
          } else {
            callback()
          }
        }).catch(({ e }) => {
          callback()
        })
      }
    }
    var validateCode = (rule, value, callback) => {
      if (validNoEmptySpace(value)) {
        callback(new Error(this.$i18n.t('shopProcess.inputAllSpace')))
      } else {
        this.$http({
          url: this.$http.adornUrl('/platform/blockchain/checkBlockchainCode'),
          method: 'GET',
          params: this.$http.adornParams({
            code: value,
            id: this.dataForm.id
          })
        }).then(({ data }) => {
          if (data) {
            callback(new Error(this.$t('shopProcess.shopNameRule')))
          } else {
            callback()
          }
        }).catch(({ e }) => {
          callback()
        })
      }
    }
    return {
      show: true,
      visible: false,
      dataForm: {
        id: 0,
        name: '',
        code: '',
        icon: '',
        address: '',
        mnemonic: '',
        middleAddress: '',
        middleMnemonic: '',
        apiUser: '',
        apiPassword: '',
        status: 1,
        orderNum: ''
      },
      isSubmit: false,
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10 // 每页显示多少条
      },
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      dataRule: {
        name: [
          { required: true, message: this.$i18n.t('chain.nameCannotBeEmpty'), trigger: 'blur' },
          {validator: validateName, trigger: 'blur'}
        ],
        code: [
          { required: true, message: this.$i18n.t('chain.codeCannotBeEmpty'), trigger: 'blur' },
          {validator: validateCode, trigger: 'blur'}
        ],
        icon: [
          { required: true, message: this.$i18n.t('chain.iconCannotBeEmpty'), trigger: 'blur' }
        ]
      }
    }
  },
  components: {
    ImgsUpload
  },
  watch: {
    visible: function () {
      if (this.visible === false) {
        this.prodsSelectVisible = false
      }
    }
  },
  methods: {
    // 获取数据列表
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.isSubmit = false
      this.show = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl('/platform/blockchain/info/' + this.dataForm.id),
            method: 'get',
            params: this.$http.adornParams()
          }).then(({ data }) => {
            this.dataForm = data
          })
        } else {
          this.$refs['dataForm'].resetFields()
        }
      })
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          if (this.isSubmit) {
            return false
          }
          this.isSubmit = true
          this.$http({
            url: this.$http.adornUrl(`/platform/blockchain`),
            method: this.dataForm.id != 0 ? 'put' : 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({ data }) => {
            this.$message({
              message: this.$i18n.t('publics.operation'),
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.dataForm.couponProds = []
                this.$emit('refreshDataList', this.page)
                this.visible = false
              }
            })
          }).catch((e) => {
            this.isSubmit = false
          })
        }
      })
    },
    beforeClose (done) {
      this.show = false
      done()
    }
  }
}
</script>
<style lang="scss" scoped>
.mod-coupon-add-or-update {
  .el-col{
    width: 130px;
  }
}
.prod-line-height {
  line-height: 18px;
}
.prod-text-left {
  margin-left: 10px;
}
.el-date-editor >>>.el-input__suffix>.el-icon-circle-close {
  display: none
}
.el-date-editor >>>.el-input__suffix>.el-icon-circle-check {
  display: none
}
</style>
